package com.dmzc.stage2.chapter22;

import java.io.IOException;
import java.util.Scanner;

public class DocumentEditThread extends Thread {
    private final String path;
    private final String name;
    private final Scanner scanner = new Scanner(System.in);

    public DocumentEditThread(String path, String name) {
        super("DocumentEditThread");
        this.path = path;
        this.name = name;
    }

    @Override
    public void run() {
        int times = 0;
        try {
            Document document = Document.create(path, name);
            while (true) {
                String text = scanner.next();
                if ("quit".equals(text)) {
                    document.close();
                    break;
                }
                document.edit(text);
                if (times % 5 == 0) {
                    document.save();
                }
                times++;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
